package cn.znzz.cjm.quartz.service.impl;

import cn.znzz.cjm.quartz.job.MainJob;
import cn.znzz.cjm.quartz.service.IQuartzService;
import cn.znzz.cjm.quartz.util.QuartzJobInfo;
import cn.znzz.cjm.quartz.util.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;

@Service
public class QuartzServiceImpl implements IQuartzService {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    @Override
    public void addJob(QuartzJobInfo jobInfo) {
        Scheduler sched=schedulerFactoryBean.getScheduler();
        String jobName=jobInfo.getJobName();
        Class cls= MainJob.class;
        Object params=jobInfo.getParams();
        String time=jobInfo.getCronj();
        QuartzUtils.addJob(sched,jobName,cls,params,time);
        System.out.println("动态添加成功"+jobInfo.getJobName());
    }

    @Override
    public void removeJob(String jobName) {
        Scheduler sched=schedulerFactoryBean.getScheduler();
        QuartzUtils.removeJob(sched,jobName);
        System.out.println("删除定时任务成功"+jobName);
    }
}
